/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.xtd.view.retail;

import com.xtd.facede.retail.AccountFacade;
import com.xtd.model.retail.Account;
import com.xtd.view.AbstractManager;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.validator.ValidatorException;

/**
 *
 * @author Lu Yuanbin
 */
@ManagedBean
@SessionScoped
public class AccountManager extends AbstractManager {

    @EJB
    AccountFacade accountFacade;
    private List<Account> accounts;
    private Account selectedAccount;
    private String searchContent;

    /** Creates a new instance of AccountManager */
    public AccountManager() {
    }

    /**
     * @return the accounts
     */
    public List<Account> getAccounts() {
        if (accounts == null) {
            accounts = new ArrayList();

        }

        return accounts;
    }

    /**
     * @param accounts the accounts to set
     */
    public void setAccounts(List<Account> accounts) {
        this.accounts = accounts;
    }

    /**
     * @return the selectedAccount
     */
    public Account getSelectedAccount() {
        return selectedAccount;
    }

    /**
     * @param selectedAccount the selectedAccount to set
     */
    public void setSelectedAccount(Account selectedAccount) {
        this.selectedAccount = selectedAccount;
    }

    public void search(ActionEvent actionEvent) {

        this.getAccounts().clear();
        this.getAccounts().addAll(accountFacade.find(searchContent));

    }

    /**
     * @return the searchContent
     */
    public String getSearchContent() {
        return searchContent;
    }

    /**
     * @param searchContent the searchContent to set
     */
    public void setSearchContent(String searchContent) {
        this.searchContent = searchContent;
    }

    public void checkUniqueMPhone(FacesContext context, UIComponent component, Object newValue) {
        if (!accountFacade.checkUnique(selectedAccount, "mphone", (String) newValue, selectedAccount.isNew())) {
            throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "保存失败", "男宾电话有重复"));
        }
    }

    public void checkUniqueFPhone(FacesContext context, UIComponent component, Object newValue) {
        if (!accountFacade.checkUnique(selectedAccount, "fphone", (String) newValue, selectedAccount.isNew())) {
            throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "保存失败", "女宾电话有重复"));
        }
    }

    public void save(ActionEvent actionEvent) {

        try {
            accountFacade.edit(selectedAccount);
            addMessage("保存成功", null);
        } catch (Exception e) {
            getLogger(this.getClass()).log(Level.FINEST, "客户档案更新失败", e);
            addMessage("保存失败", "请稍候再试");
        }

    }
}
